/*
 * FileName：VoteIndexCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.wx.ctrl;

import com.smart.common.BaseCtrl;
import com.smart.common.util.AjaxResult;
import com.smart.common.util.WxMemoryCacheClient;
import com.smart.wx.wxcms.entity.Vote;
import com.smart.wx.wxcms.entity.VoteApply;
import com.smart.wx.wxcms.entity.VoteRecord;
import com.smart.wx.wxcms.service.VoteApplyService;
import com.smart.wx.wxcms.service.VoteRecordService;
import com.smart.wx.wxcms.service.VoteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 投票微信端
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-11 20:58:43
 */
@Controller
@RequestMapping("/front/voteindex")
public class VoteIndexCtrl extends BaseCtrl {

    @Resource
    private VoteService voteService;

    @Resource
    private VoteApplyService voteApplyService;

    @Resource
    private VoteRecordService voteRecordService;


    /**
     * 获取投票页面 [该url 配置到拦截器中]
     */
    @RequestMapping("/getVoteIndex/{account}/{voteId}")
    public String getVoteIndex(@PathVariable String account,@PathVariable String voteId) throws Exception {
        return "redirect:/views/front/vote/vote.html?account=" + account + "&voteId=" + voteId;
    }

    /**
     * 获取投票报名页面  [该url 配置到拦截器中]
     */
    @RequestMapping("/getVoteApplyIndex/{account}/{voteId}")
    public String getVoteApplyIndex(@PathVariable String account,@PathVariable String voteId) throws Exception {
        return "redirect:/views/front/vote/apply.html?account=" + account + "&voteId=" + voteId;
    }


    /**
     * 投票信息(报名人数、总投票数、粉丝参与人数)
     */
    @ResponseBody
    @RequestMapping("/getVoteInfo")
    public AjaxResult getVoteStatInfo(Vote vote) throws Exception {

        /******* 报名人数 ********/
        VoteApply voteApply = new VoteApply();
        voteApply.setVoteId(vote.getId());
        int applyCount = voteApplyService.queryCount(voteApply);

        /******* 总投票人数 ********/
        VoteRecord voteRecord = new VoteRecord();
        voteRecord.setVoteId(vote.getId());
        int voteCount = voteRecordService.getVoteCountByVoteId(voteRecord);

        /******* 粉丝参与人数 ********/
        int fansCount = this.voteRecordService.getFansCountByVoteId(voteRecord);

        /******* 投票的基本信息 ********/
        vote = voteService.queryObject(vote);
        vote.setVoteState(this.getVoteState(vote));
        vote.setApplyState(this.getApplyState(vote));

        vote.setApplyCount(applyCount);
        vote.setVoteCount(voteCount);
        vote.setFanCount(fansCount);

        // 如果当前用户已经报名 则报名链接应该显示 已报名  0 未报名 1 已报名
        String sessionid = request.getSession().getId();
        String openId = WxMemoryCacheClient.getOpenid(sessionid);

        VoteApply tempBean = new VoteApply();
        tempBean.setOpenId(openId);
        tempBean.setVoteId(vote.getId());

        tempBean = this.voteApplyService.queryObject(tempBean);
        if(tempBean != null){ // 说明已经报名
            vote.setPersonalApplyState("1");
        }else{
            vote.setPersonalApplyState("0");
        }

        return AjaxResult.success(vote);
    }

    // 状态：0未发布 1已发布 2已结束
    private int getVoteState(Vote vote){
        int state = vote.getVoteState();

        // 当前时间
        Date nowDate = new Date();

        Date startTime = vote.getVoteStartTime();
        Date endTime = vote.getVoteEndTime();

        if(nowDate.getTime() >= startTime.getTime() && nowDate.getTime() <= endTime.getTime()){

            state = 1;
        }
        if(nowDate.getTime() > endTime.getTime()){
            state = 2;
        }

        return state;
    }


    // 报名状态：0未开始 1已开始(可以报名) 2已结束
    private String getApplyState(Vote vote){
        String state = "0";

        // 当前时间
        Date nowDate = new Date();

        Date startTime = vote.getSignUpStartTime();
        Date endTime = vote.getSignUpEndTime();

        if(nowDate.getTime() >= startTime.getTime() && nowDate.getTime() <= endTime.getTime()){

            state = "1";
        }
        if(nowDate.getTime() > endTime.getTime()){
            state = "2";
        }


        return state;
    }



    /**
     * 获取报名人员列表(供用户投票)(分页)
     */
    @ResponseBody
    @RequestMapping("/getVoteApplyList")
    public AjaxResult getVoteApplyList(VoteApply voteApply) throws Exception {

        voteApply.setPageSize(8);

        List<VoteApply> voteApplyList = voteApplyService.getVoteApplyListByPage(voteApply);
        return getResult(voteApply, voteApplyList);
    }

    /**
     * 投票报名
     */
    @ResponseBody
    @RequestMapping("/voteApplySave")
    public AjaxResult voteApplySave(VoteApply voteApply) throws Exception {

        String sessionid = request.getSession().getId();
        String openId = WxMemoryCacheClient.getOpenid(sessionid);
        voteApply.setOpenId(openId);

        return this.voteApplyService.save(voteApply);
    }

    /**
     * 用户投票
     */
    @ResponseBody
    @RequestMapping("/voteRecordSave")
    public AjaxResult voteRecordSave(VoteRecord voteRecord) throws Exception {

        String sessionid = request.getSession().getId();
        String openId = WxMemoryCacheClient.getOpenid(sessionid);
        voteRecord.setOpenId(openId);

        return this.voteRecordService.save(voteRecord);
    }
}
